package com.example.swim.Service;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.swim.Dao.ClassRoomDao;
import com.example.swim.Pojo.Activity;
import com.example.swim.Pojo.ClassRoom;
import com.example.swim.Pojo.Coach;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 *
 *
 * @author LiYa
 * @create 2022-07-14 12:48
 *
 *
 * /**
 *      * @Cacheable 开启基于注解的缓存功能
 *      *
 *      * @Cacheable注解 ：先从redis数据库中 按照当前key查找，有没有。如果没有再调用方法返回结果，如果结果不为null将其缓存到数据库中
 *      *      属性：
 *      *      value：key的一部分（前缀），主要是指明数据放在那个key范围
 *      *      key：key的主体，#p0：指明取出第一个参数  #p1：指明取出第二个参数。。。依此类推
 *      *      unless：结果为true，将当前的数据结果不保存到redis，#result：指明取出数据库中返回的结果
 *      *      condition 结果如果为true，将当前数据保存到redis
 */
@Service
public class ClassRoomService {
    @Autowired
    ClassRoomDao classRoomDao;

    @Cacheable(value = "allCR" ,key = "'findCR'")
    public List<ClassRoom> findAll()
    {
        return classRoomDao.findAll();
    }
    @Cacheable(value = "allCR" ,key = "#p0")
    public ClassRoom findByName(Integer id)
    {
        return classRoomDao.findByName(id);
    }
    @Cacheable(value="allCR",key="#coach.getId()")
    public List<ClassRoom> findByCoach_id(Coach coach){
        return classRoomDao.findByCoach_id(coach);
    }
    @Cacheable(value="allCR",key="#p0")
    public String findCoach(String name){
        return classRoomDao.findCoach(name);
    }
    @CachePut(value = "allCR",key = "'findCR'")
    public List<ClassRoom>  insertClassRoom(ClassRoom cr){
        classRoomDao.insertClassRoom(cr);
        return classRoomDao.findAll();
    }
    @CacheEvict(value = "allCR",allEntries = true)
    public int  delClassRoom(Integer id)
    {
        return classRoomDao.delClassRoom(id);
    }

    @CacheEvict(value = "allCR",key = "'findCR'")
    public List<ClassRoom> updateClassRoom(ClassRoom cr)
    {
        classRoomDao.updateClassRoom(cr);
        return classRoomDao.findAll();
    }
    @CacheEvict(value = "allCR",key = "'findCR'")
    public List<ClassRoom> updateNum(Integer cid)
    {
        classRoomDao.updateNum(cid);
        return classRoomDao.findAll();
    }

    public String findAge(int cid)
    {
        ClassRoom cr=classRoomDao.selectById(cid);
        return cr.getAge();
    }
    public List<ClassRoom> find(String type, String kind){
        HashMap<String,Object> map=new HashMap<String,Object>();
        map.put("type", type);
        map.put("kind", kind);
        List<ClassRoom> classRooms= classRoomDao.selectByMap(map);
        return  classRooms;
    }
}
